//
//  CookBookViewController.m
//  Delicious2
//
//  Created by dllo on 16/1/7.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "CookBookViewController.h"

@interface CookBookViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, PicPushWebViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) NSArray *photoDataSource;
@property (nonatomic, strong) PhotoClass *photoClass;

@property (nonatomic, strong) MBProgressHUD *HUD;
@property (nonatomic, strong) UIButton *buttonReGetData;

@end

@implementation CookBookViewController
//去除通知
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"leftBarItemButtonImage"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemDidPress:)];
    self.navigationItem.title = @"食谱";
    
  
    [self getCookBookData];
    [self creatCollectionView];
 
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载";
    
    //leftVC跳转通知
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(jumpLeftGroupViewController:) name:@"Jump" object:nil];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.view nightMode];
    [self.collectionView nightMode];
    [self.collectionView reloadData];
}

#pragma mark - creatSubviews
//创建CollectionView
- (void)creatCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.itemSize = CGSizeMake(WIDTH , HEIGHT / 3 * 2 - 113);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, HEIGHT / 3, WIDTH, HEIGHT / 3 * 2) collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    
    [_collectionView registerClass:[CookCustomCollectionViewCell class] forCellWithReuseIdentifier:@"CookCustomCollectionViewCellIdentifier"];
   
    
    self.collectionView.bounces = NO;
    [self.view addSubview:_collectionView];
  
}

//轮播图
- (void)creatPhotoClass {
    //数组保护
    if (self.photoDataSource.count != 0) {
        NSMutableArray *arrayUrl = [NSMutableArray arrayWithCapacity:0];
        NSMutableArray *arrayTitle = [NSMutableArray arrayWithCapacity:0];
        NSMutableArray *arrayId = [NSMutableArray arrayWithCapacity:0];
        
        for (int i = 0; i < self.photoDataSource.count; i++) {
            CookBookPhotoModel *photoModel = self.photoDataSource[i];
            [arrayUrl addObject:photoModel.image_url];
            [arrayTitle addObject:photoModel.food_name];
            [arrayId addObject:photoModel.food_code];
        }
        //初始化轮播图
        self.photoClass = [[PhotoClass alloc] initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height + 20, WIDTH, HEIGHT / 3) withArray:arrayUrl withArrayTitle:arrayTitle withArrayId:arrayId withTopModelArray:nil];
        self.photoClass.kind = @"食谱";
        self.photoClass.delegate = self;
        [self.view addSubview:self.photoClass];
    }
}



#pragma mark - collectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CookCustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CookCustomCollectionViewCellIdentifier" forIndexPath:indexPath];
    
    CookBookModel *cookModel = self.dataSource[indexPath.item];
    cell.cookModel = cookModel;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //跳转专题页
    CookBookModel *cookModel = self.dataSource[indexPath.item];
    
    CookBookTopicViewController *topicVC = [[CookBookTopicViewController alloc] init];
    topicVC.strId = cookModel.id.stringValue;
    
    [self.navigationController pushViewController:topicVC animated:YES];
    
}


#pragma mark - 请求数据
- (void)getCookBookData {
    
    //轮播图数据
    [FSDNetWorkTool getWithURL:kCookBookPhotoGETURL withParameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        NSArray *arr = result[@"others"];
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in arr) {
            CookBookPhotoModel *photoModel = [[CookBookPhotoModel alloc] initWithDictionary:dic];
            [array addObject:photoModel];
        }
        self.photoDataSource = [NSArray arrayWithArray:array];
        
        [self creatPhotoClass];
    } fail:^(NSError *error) {
        NSLog(@"错误");
    }];
    
    //首页数据
    [FSDNetWorkTool getWithURL:kCookBookGETURL withParameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        NSArray *arr = result[@"topics"];
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in arr) {
            CookBookModel *cookModel = [[CookBookModel alloc] initWithDictionary:dic];
            [array addObject:cookModel];
        }
        self.dataSource = [NSArray arrayWithArray:array];
        
        self.HUD.hidden = YES;
        [self.collectionView reloadData];
    } fail:^(NSError *error) {
        NSLog(@"首页请求错误");
        
        self.HUD.hidden = YES;
        //重新加载按钮
        self.buttonReGetData = [UIButton buttonWithType:UIButtonTypeSystem];
        self.buttonReGetData.frame = CGRectMake(0, 0, 100, 100);
        self.buttonReGetData.layer.cornerRadius = 10;
        self.buttonReGetData.center = self.view.center;
        [self.buttonReGetData setTitle:@"重新加载" forState:UIControlStateNormal];
        [self.buttonReGetData setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.buttonReGetData.backgroundColor = [UIColor blackColor];
        self.buttonReGetData.alpha = 0.6;
        
        [self.buttonReGetData addTarget:self action:@selector(buttonReGetDataAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:self.buttonReGetData];
        
        
        
    }];
}

//左侧按钮点击事件
- (void)leftBarButtonItemDidPress:(UIBarButtonItem *)sender{
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    DrawerViewController *drawerVC = delegate.drawerVC;
    [drawerVC showLeftViewController:true];

}

#pragma mark - leftVC通知方法
- (void)jumpLeftGroupViewController:(NSNotification *)message {
    
    //跳转分类页面
    NSString *str;
    LeftGroupViewController *groupVC = [[LeftGroupViewController alloc] init];
    groupVC.strKind = message.object;
    
    if ([message.object isEqualToString:@"group"]) {
        str = @"类别";
    } else if ([message.object isEqualToString:@"cooking"]) {
        str = @"烹饪方式";
    } else if ([message.object isEqualToString:@"brand"]) {
        str = @"品牌";
    } else if ([message.object isEqualToString:@"restaurant"]) {
        str = @"连锁餐饮";
    } else if ([message.object isEqualToString:@"season"]) {
        str = @"季节";
    }
    groupVC.navigationItem.title = str;
    self.tabBarController.selectedIndex = 0;
    [self.navigationController pushViewController:groupVC animated:YES];
    
    //隐藏抽屉
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    DrawerViewController *drawerVC = delegate.drawerVC;
    [drawerVC hideSideViewController:YES];
}

#pragma mark - 轮播图跳转
-  (void)picPushWebView:(NSString *)strTitle withArrayId:(NSString *)strId {
    
    CookDetailsViewController *detaileVC = [[CookDetailsViewController alloc] init];
    detaileVC.strCode = strId;
    
    [self.navigationController pushViewController:detaileVC animated:YES];
}

#pragma mark -- buttonReGetData
//重新请求数据
- (void)buttonReGetDataAction:(UIButton *)button {
    [self getCookBookData];
    button.hidden = YES;
}


@end
